# -*- coding:utf-8 -*-
"""Created By Corvo"""

from mod.common.minecraftEnum import ActorDamageCause

from ..common.base import CombineEntityBaseNew
from ...mdk.common.system.base import *
from ...mdk.server.entity import *


class ThunderRaccoon(CombineEntityBaseNew):

    def __init__(self, entityId):
        super(ThunderRaccoon, self).__init__(entityId)
        self.data_key = "ThunderRaccoonCombineStorage"
        self.combine_info = self.GetStorage(self.data_key).get("element_mob", [])  # type: list
        self.AddEffect(GameEffect.FIRE_RESISTANCE, 99999, 0, False)

    def ConfigEvent(self):
        super(ThunderRaccoon, self).ConfigEvent()
        self.serverEvent["OnTransformToThunderRaccoonEvent"] = self.OnTransformToThunderRaccoonEvent

    def ActiveAttack(self):
        super(ThunderRaccoon, self).ActiveAttack()
        self.SetPlayMusic("beast.thunder_raccoon_attack")
        self.skill_module.CastSkill(self.id, "thunder_raccoon_attack", {
            "attack_target": self.GetAttackTarget(),
            "damage": self.attack
        })
        self.game_comp.AddTimer(1.4, self.OnSkillFinished)

    def ActiveSkill1(self):
        super(ThunderRaccoon, self).ActiveSkill1()
        self.SetPlayMusic("beast.thunder_raccoon_sk1")
        self.skill_module.CastSkill(self.id, "thunder_raccoon_sk1", {
            "attack_target": self.GetAttackTarget(),
            "radius": int(5 + self.GetCollisionBoxSize()[0] * 1.8)
        })
        self.game_comp.AddTimer(3.0, self.OnSkillFinished)

    def ActiveSkill2(self):
        super(ThunderRaccoon, self).ActiveSkill2()
        self.skill_module.CastSkill(self.id, "thunder_raccoon_sk2", {
            "attack_target": self.GetAttackTarget(),
            "offset": [0, self.GetCollisionBoxSize()[1] * 0.35, 4.25],
            "radius": int(2 + self.GetCollisionBoxSize()[0] * 1.5)
        })
        self.game_comp.AddTimer(1.0, self.OnSkillFinished)

    def OnDeath(self, attacker):
        self._DestroyAllGen()
        if not self.GetTameOwner():
            # 战利品
            for entity in self.combine_info:
                item_name = entity.replace("beast:", "beast:essential_")
                if self.Item.LookupItemByName(item_name):
                    self.Item.SpawnAtPosByName(item_name, pos=self.foot_pos, dim=self.dim)
            return
        self.NotifyToClient(self._owner_player, ServerEvent.RequestBindLockAimEntityEvent, {
            "show": False
        })
        for mob_type in self.combine_info:
            self.AddEntity(mob_type, self.foot_pos, self.dim)

    # -----------------------------------------------------------------------------------

    @check_entity_id
    def OnTransformToThunderRaccoonEvent(self, args):
        element_mob = args["element_mob"]
        owner_id = args["ownerId"]
        self.SetStorage(self.data_key, {"element_mob": element_mob})
        self.combine_info = element_mob

        self._owner_player = owner_id

        self.BroadcastEvent("OnFinishedTransformToThunderRaccoonEvent", {
            "playerId": self._owner_player,
            "entityId": self.id
        })

    def OnActuallyTookDamage(self, args):
        super(ThunderRaccoon, self).OnActuallyTookDamage(args)
        entityId = args["srcId"]
        spawn_pos = RawEntity.GetPos(entityId)
        RawEntity.CreateRaw("minecraft:lightning_bolt", pos=spawn_pos, dim=self.dim)

    def OnTookDamage(self, args):
        super(ThunderRaccoon, self).OnTookDamage(args)
        if args["cause"] == ActorDamageCause.Lightning:
            args["damage"] = 0
            args["knock"] = False
            args["ignite"] = False
